<?php
/**
 * 驾校学员管理
 * 考试进度跟进
 * 学员状态更改
 */
#定义命名空间
namespace Admin\Controller;
class StudentController extends ComController{
    
    /**
     * [index 已报名缴费学员列表]
     */
    public function index(){
        $sign_record = M('sign_record');//实例化报名表
        $d_id = $this->USER['uid'];
        $prefix = C('DB_PREFIX');
        $p = I('get.p') ? I('get.p') : 1;
        $class_id = I('get.class_id') ? I('get.class_id') : 0;
        $phone = I('get.phone') ? I('get.phone') : 0;
        $name = I('get.name') ? I('get.name') : '';
        $pagesize = 16;//每页显示条数
        $limit = ($p-1)*$pagesize;//偏移量
        $count_where["is_paid"] = 1;//已付款
        $count_where["d_id"] = $d_id;//驾校ID
        $count = $sign_record->where($count_where)->count();
        $where["{$prefix}sign_record.d_id"] = $d_id;
        $where["{$prefix}sign_record.is_paid"] = 1;
        if($class_id)
            $where["{$prefix}sign_record.c_id"] = $class_id;
        if($phone)
            $where["{$prefix}sign_record.phone"] = $phone;
        if($name)
            $where["{$prefix}sign_record.user_name"] = array('like',"%{$name}%");
        $student = $sign_record
                    ->field("{$prefix}sign_record.*,{$prefix}class_level.type,{$prefix}class_level.name as class_name")
                    ->join("{$prefix}class_level ON {$prefix}class_level.id = {$prefix}sign_record.c_id")
                    ->where($where)
                    ->order('id desc')
                    ->limit($limit.','.$pagesize)
                    ->select();
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        //班级
        $class_level = M('class_level');
        $class_data = $class_level->where(array('d_id'=>$d_id))->select();
        $this->assign('class_level',$class_data);
        $this->assign('page',$page);
        $this->assign('data',$student);
        $this->display();   
    }

    /**
     * ajax更改学员学车进度
     */
    public function schedule(){
        $param = I('param') ? I('param') : '';
        if($param){
            $param = explode('-',$param);
            $schedule = $param['0'];
            $id = $param['1'];
            $sign = M('sign_record')->where(' id = '.$id)->save(array('schedule'=>$schedule));
            if($sign !== false){
                echo json_encode(array('res'=>1,'msg'=>'修改成功'));die;
            }else{
               echo json_encode(array('res'=>0,'msg'=>'修改失败'));die; 
            }
        }else{
            echo json_encode(array('res'=>0,'msg'=>'参数错误'));die;
        }
    }


    /**
     * ajax更改学员状态
     */
    public function status(){
        $param = I('param') ? I('param') : '';
        if($param){
            $param = explode('-',$param);
            $status = $param['0'];
            $id = $param['1'];
            $sign = M('sign_record')->where(' id = '.$id)->save(array('status'=>$status));
            if($sign !== false){
                echo json_encode(array('res'=>1,'msg'=>'修改成功'));die;
            }else{
               echo json_encode(array('res'=>0,'msg'=>'修改失败'));die; 
            }
        }else{
            echo json_encode(array('res'=>0,'msg'=>'参数错误'));die;
        }
    }
}